package com.hoku.auth.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

import javax.servlet.http.HttpServletResponse;

/**
 * * 因为提供了用户信息/user/info接口，所以也是资源服务器EnableResourceServer
 * @author ZHUFEIFEI
 */
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {


    @Value("${management.endpoints.web.base-path}")
    private String managementContextPath;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //健康检查相关的允许直接访问
        http.authorizeRequests().antMatchers(managementContextPath+"/**")
                .permitAll();
        http
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((request, response, authException)
                        -> response.sendError(HttpServletResponse.SC_UNAUTHORIZED))
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .httpBasic();
    }
}
